package project3;

import edu.princeton.cs.algs4.*;
import project3.dijkstra.DijkstraSp1;

import java.io.File;
import java.util.Scanner;

/**
 * 任意点对之间的距离,仅打印距离
 * */
public class Distences {

    public static void findRoute(EdgeWeightedGraph mDigraph,
                                 Point[] points){
        Scanner scanner = new Scanner(System.in);

        DijkstraSp1 msp = new DijkstraSp1(mDigraph,points);
        int n = 10;
        while (n>0) {
            int s = scanner.nextInt();
            int d = scanner.nextInt();
            long start_time = System.currentTimeMillis();
            msp.setFrom(s);
            msp.setTo(d);
            boolean b = msp.hasPathTo(d);
            if (b) {
                long consumingtime=System.currentTimeMillis()-start_time;
                System.out.println("运行时间："+consumingtime+"ms");
                double distences = 0;
                //System.out.println("最短路径为：");
                for (Edge edge : msp.pathTo(d)) {
                    //System.out.println(edge + " ");
                    distences+=edge.weight();
                }
                System.out.println("最短路径长度为："+distences);
                //System.out.println(s + "->" + d + "最短路径长度为：" + msp.distTo(d));
            } else {
                long consumingtime=System.currentTimeMillis()-start_time;
                System.out.println("运行时间："+consumingtime+"ms");
                System.out.println("不存在一条路径");
            }
            n--;
        }

    }

    public static void main(String[] args) {
        Test3 test3 = new Test3();
        File file = new File("D:/my_code/Algorithm4/AlgorithmHomework/resources/usa.txt");
        In in = new In(file);
        int a = in.readInt();//点数
        int b = in.readInt();//边数
        EdgeWeightedGraph mDigraph = new EdgeWeightedGraph(a);
        Point[] points = new Point[a];
        test3.init_Point(in, points);
        test3.init_Graph(in, b, mDigraph, points);
        findRoute(mDigraph, points);
    }
}
